package org.dcm4che3.tool.print.entity;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.dcm4che3.data.UID;
import org.dcm4che3.tool.printscp.enums.DBType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Config {
	private final static Logger logger = LoggerFactory.getLogger(Config.class);
	
	private JDBCInfo jdbcInfo;
	
	private String dicomStoreDir = "E:\\printtest";
	private boolean isGray = true;
	private JSONObject DEST_AE_CONFIG = null;
	private boolean doTransfer = false;
	private String transferExePath = "";
	private int threadCount = 16;
	
	private String printerSopClasses = UID.PrinterSOPClass;
	public String getPrinterSopClasses() {
		return printerSopClasses;
	}

	public String getSessionSopClasses() {
		return sessionSopClasses;
	}

	public String getFilmBoxSopClasses() {
		return filmBoxSopClasses;
	}

	public String getImageBoxSopClasses() {
		return imageBoxSopClasses;
	}
	
	public boolean isDoTransfer() {
		return this.doTransfer;
	}
	
	public String getTransferExePath() {
		return this.transferExePath;
	}

	private String sessionSopClasses = UID.BasicFilmSessionSOPClass;
	private String filmBoxSopClasses = UID.BasicFilmBoxSOPClass + "," + UID.PresentationLUTSOPClass;
	private String imageBoxSopClasses = UID.BasicGrayscaleImageBoxSOPClass + "," + UID.BasicColorImageBoxSOPClass;

	public String getDicomStoreDir() {
		return dicomStoreDir;
	}

	public void setDicomStoreDir(String dicomStoreDir) {
		this.dicomStoreDir = dicomStoreDir;
	}
	
	public JSONObject getAeCustomConfig() {
		return DEST_AE_CONFIG;
	}

	public Config() {
		
	}
	
	public Config(String file) throws Exception {
		Properties properties = new Properties();
		properties.load(new FileInputStream(new File(file)));
		setDicomStoreDir(properties.getProperty("dicomStoreDir"));
		logger.info("use custom store dir={} to store dicom", getDicomStoreDir());
		
		this.isGray = Boolean.getBoolean(properties.getProperty("isGray","true"));
		this.doTransfer = properties.getProperty("do.transfer").equals("true");
		this.transferExePath = properties.getProperty("transferExePath");
		String url = properties.getProperty("jdbc.url");
		String	userName = properties.getProperty("jdbc.userName");
		String	password = properties.getProperty("jdbc.password");
		int maxActive = Integer.parseInt(properties.getProperty("jdbc.password", "20"));
		jdbcInfo = new JDBCInfo(url, userName, password, maxActive);
		logger.info("do transfer={}, from file={} ", doTransfer, file);
		if (properties.getProperty("ae.config") != null) {
			DEST_AE_CONFIG = new JSONObject();
			JSONArray aeArray = JSONArray.parseArray(properties.getProperty("ae.config"));
			//[{"local":{"ae":"ftimage","ip":"192.168.0.1","port":1111},"remote":{"ae":"ftimage","ip":"192.168.0.2","port":1111}}]
			for (int i = 0; i < aeArray.size(); i++) {
				DEST_AE_CONFIG.put(aeArray.getJSONObject(i).getJSONObject("local").getString("ae"), aeArray.getJSONObject(i));
			}
		}
		if (properties.getProperty("print.printerSopClasses") != null && !"".equals(properties.getProperty("print.printerSopClasses"))) {
			logger.info("use custom printer sop class=" + properties.getProperty("print.printerSopClasses"));
			this.printerSopClasses =  properties.getProperty("print.printerSopClasses");
		}
		
		if (properties.getProperty("print.sessionSopClasses") != null && !"".equals(properties.getProperty("print.sessionSopClasses"))) {
			logger.info("use custom sessionSopClasses =" + properties.getProperty("print.sessionSopClasses"));
			this.sessionSopClasses = properties.getProperty("print.sessionSopClasses");
		}

		if (properties.getProperty("print.filmBoxSopClasses") != null && !"".equals(properties.getProperty("print.filmBoxSopClasses"))) {
			logger.info("use custom filmBoxSopClasses =" + properties.getProperty("print.filmBoxSopClasses"));
			this.filmBoxSopClasses = properties.getProperty("print.filmBoxSopClasses");
		}

		if (properties.getProperty("print.imageBoxSopClasses") != null && !"".equals(properties.getProperty("print.imageBoxSopClasses"))) {
			logger.info("use custom imageBoxSopClasses =" + properties.getProperty("print.imageBoxSopClasses"));
			this.imageBoxSopClasses = properties.getProperty("print.imageBoxSopClasses");
		}
		
		if (properties.getProperty("process.thread.count") != null && !"".equals(properties.getProperty("process.thread.count"))) {
			this.threadCount = Integer.parseInt(properties.getProperty("process.thread.count"));
		}
	}
	
	public int getProcessThreadCount() {
		return this.threadCount;
	}
	
	public boolean isGray() {
		return isGray;
	}

	public class JDBCInfo {
		private String url;
		private String userName;
		private String password;
		private int maxActive = 20;
		public JDBCInfo(String url, String userName, String password, int maxActive) {
			this.url = url;
			this.userName = userName;
			this.password = password;
			this.maxActive = maxActive;
		}
		public String getUrl() {
			return url;
		}
		public String getUserName() {
			return userName;
		}
		public String getPassword() {
			return password;
		}
		public int getMaxActive() {
			return maxActive;
		}
	}
}
